﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows;
using System.Timers;
using System.Windows.Media.Animation;

namespace StarField
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int step = 15;
        public int dx = 0;
        public int dy = 0;
        Timer t;
        private TranslateTransform upTransform = new TranslateTransform();
         
        public MainWindow()
        {
            InitializeComponent();
            t = new Timer();
             
            t.Elapsed += new ElapsedEventHandler(delegate(object sender, ElapsedEventArgs e) 
                {
                     
                 starShip.Dispatcher.BeginInvoke((Action)(()=>{
                    Canvas.SetTop(starShip, Canvas.GetTop(starShip) + dy);
                    Canvas.SetLeft(starShip, Canvas.GetLeft(starShip) + dx);}));
                    
                });
            t.Interval = 50;
            t.Enabled = true;
            t.AutoReset = true;
            t.Start();
           
        }

        private void Image_KeyDown(object sender, KeyEventArgs e)
        {
           // MessageBox.Show(e.Key.ToString());
            if (e.Key == Key.Up)
            {
                DoubleAnimation oLabelAngleAnimation = new DoubleAnimation();
                oLabelAngleAnimation.From = 0;
                oLabelAngleAnimation.To = -480;
                oLabelAngleAnimation.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 500));
                oLabelAngleAnimation.RepeatBehavior = new RepeatBehavior(100);
                TranslateTransform oTransform = new TranslateTransform();
                starShip.RenderTransform = oTransform;
                
                oTransform.BeginAnimation(TranslateTransform.YProperty, oLabelAngleAnimation);
            }
            else if (e.Key == Key.Down)
            {
               
 
            }
            else if (e.Key == Key.Left)
            {
                dx = -step;
                
            }
            else if (e.Key == Key.Right)
            {
                dx = step;
                 
            }

        }

        private void Image_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Up)
            {
              
            }
            if (e.Key == Key.Down)
            {
                starShip.RenderTransform = null;
            }
            if (e.Key == Key.Left)
            {
                dx = 0;
                starShip.RenderTransform = new SkewTransform(0, 0);
            }
            if (e.Key == Key.Right)
            {
                dx = 0;
                starShip.RenderTransform = new SkewTransform(0, 0);
            }
        }

        private void Window_KeyDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }
    }
}
